<template>
  <!-- 添加或修改出厂试验-镀锌质量对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="抽检批次号" prop="batchNumber">
                <el-input v-model="form.batchNumber" placeholder="保存后自动生成" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生产批次号" prop="productNumber">
                <el-input v-model="form.productNumber" placeholder="请输入生产批次号"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="检验时间" prop="inspectTime">
                <el-date-picker clearable
                                v-model="form.inspectTime"
                                type="date"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="请选择检验时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="锌层厚度" prop="znThick">
                <el-input-number v-model="form.znThick" placeholder="请输入锌层厚度" :precision="2" style="width:80%;"/>
                <el-tag style="margin-left: 10px;" v-for="standard in standardList" v-if="standard.propName==='znThick'"
                        type="danger">{{ standard.propValueMin }}-{{ standard.propValueMax }}
                </el-tag>
              </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="是否合格" prop="testPass">
                  <el-radio-group v-model="form.testPass">
                    <el-radio
                      v-for="dict in dict.type.yes_no"
                      :key="dict.value"
                      :label="dict.value"
                    >{{ dict.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="质检报告" prop="reportFile">
                <file-upload v-model="form.reportFile"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <el-table border v-loading="loading" :data="dataList"  ref="table" :max-height="tableMaxHeight">
      <el-table-column label="序号" type="index"  width="55" align="center"/>
      <el-table-column label="检验时间" align="center" prop="inspectTime" width="300">
        <template slot-scope="scope">
          <el-date-picker size="mini" clearable v-model="scope.row.inspectTime" type="datetime" value-format="yyyy-MM-dd"
                          placeholder="请选择检验时间">
          </el-date-picker>
        </template>
      </el-table-column>
      <el-table-column prop="znThick" label="锌层厚度" width="300">
        <template slot-scope="scope">
          <el-input-number size="mini" v-model="scope.row.znThick"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="300">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.dun"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row,scope.$index)"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col :span="24" style="text-align: center;padding-top: 20px;">
        <el-button style="width: 100%;" icon="el-icon-plus" plain type="primary" @click="addRow">添加行</el-button>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, deleteAction, getAction, getById, isEmpty, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";

export default {
  name: "FactoryTestZnForm",
  dicts: ["yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inspectTime: [
          {required: true, message: "请输入检验时间", trigger: "change"}
        ],
        znThick: [
          {required: true, message: "请输入锌层厚度", trigger: "change"}
        ],
      },
      url: {
        getById: '/factoryTestZn/factoryTestZn/',
        add: '/factoryTestZn/factoryTestZn/add',
        edit: '/factoryTestZn/factoryTestZn/edit',
        detailList: '/factoryTestZnDetail/factoryTestZnDetail/list',
        remove: '/factoryTestZnDetail/factoryTestZnDetail/',
      },
      standardList: [],
      dataList: [],
      tableMaxHeight: window.innerHeight - 320,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        testId: null,
        inspectTime: null,
        znThick: null,
        reportFile: null,
        pictureFile: null,
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        orderId: null,
        goodsId: null,
        goodsName: null,
        goodsModel: null,
        batchNumber: null,
        productNumber: null,
        inspectTime: parseTime(new Date(), '{y}-{m}-{d}'),
        znThick: null,
        testPass: '1',
        reportFile: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.dataList = [];
      for (let i = 0; i < 1; i++) {
        this.dataList.push({
          sort:"1",
          inspectTime: parseTime(new Date(), '{y}-{m}-{d}'),
        })
      }
      this.loading=false;
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dataList=[];
      for (let i = 0; i < 1; i++) {
        this.dataList.push({
          sort:"1",
          inspectTime: parseTime(new Date(), '{y}-{m}-{d}'),
        })
      }
      this.reset();
      this.open = true;
      this.title = "添加镀锌质量";
    },
    /** 新增按钮操作 */
    handleAddByWorkOrderId(workOrderId, goodsId) {
      this.reset();
      this.form.workOrderId = workOrderId;
      this.form.goodsId = goodsId;
      this.open = true;
      this.title = "添加镀锌质量";
    },

    /** 新增按钮操作 */
    handleAddByDaohuoDetailId(daohuoDetailId, goodsId) {
      this.reset();
      this.form.daohuoDetailId = daohuoDetailId;
      this.form.goodsId = goodsId;
      this.open = true;
      this.title = "添加镀锌质量";
    },

    /** 新增按钮操作 */
    handleAddByProduceDetailId(produceRecordDetailId, goodsId) {
      this.reset();
      this.form.produceRecordDetailId = produceRecordDetailId;
      this.form.goodsId = goodsId;
      this.open = true;
      this.title = "添加镀锌质量";
    },

    /** 新增按钮操作 */
    handleAddByFahuoDetailId(fahuoDetailId) {
      this.reset();
      this.form.fahuoDetailId = fahuoDetailId;
      this.open = true;
      this.title = "添加镀锌质量";
    },


    /** 新增按钮操作 */
    handleAddByRukuId(rukuId, goodsId) {
      this.reset();
      this.form.rukuId = rukuId;
      this.form.goodsId = goodsId;
      this.open = true;
      this.title = "添加镀锌质量";
    },

    /** 新增按钮操作 */
    handleAddByGoodsSaleId(goodsSaleId, goodsId) {
      this.reset();
      this.form.goodsSaleId = goodsSaleId;
      this.form.goodsId = goodsId;
      this.open = true;
      this.title = "添加镀锌质量";
    },


    /** 新增按钮操作 */
    handleAddBySampleId(sampleId, goodsId, goodsSaleId) {
      this.reset();
      this.form.sampleId = sampleId;
      this.form.goodsId = goodsId;
      this.form.goodsSaleId = goodsSaleId;
      this.open = true;
      this.title = "镀锌质量";
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryParams.testId = row.id;
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出厂试验-镀锌质量";
      });
      this.getList();
    },

    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.detailList, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.loading = false;
        if (this.dataList.length === 0) {
          for (let i = 0; i < 1; i++) {
            this.dataList.push({
              sort:"1",
              inspectTime: parseTime(new Date(), '{y}-{m}-{d}'),
            })
          }
        }else {
          for (let i = 0; i < this.dataList.length; i++) {

          }
        }
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        let list = this.dataList;
        for (let i = 0; i < list.length; i++) {
          if(!list[i].inspectTime){
            this.$modal.msgError("序号："+i + "请选择检测时间");
            return;
          }
          if(!list[i].znThick){
            this.$modal.msgError("序号："+i + "请输入锌厚度");
            return;
          }
        }

        this.form.detailList = this.dataList;

        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    //点击按钮添加空行
    addRow() {
      this.$nextTick(() => {
        this.dataList.push({
          sort:this.dataList.length+1,
          inspectTime: parseTime(new Date(), '{y}-{m}-{d}'),
        })
      })
    },

    handleDelete(row,index) {
      const ids = row.id || this.ids;
      if(isEmpty(ids)){
        this.$nextTick(() => {
          this.dataList.splice(index, 1)
        })
      }else{
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    },



  }
};
</script>
